/*
 * @Author: yangy
 * @Date: 2023-11-07 14:13:26
 * @LastEditors: yangy aidreams@sina.com
 * @LastEditTime: 2024-03-08 16:11:34
 * @FilePath: /qn_mall_frontend/src/utils/util.js
 * @Description:
 *
 * Copyright (c) 2023 by 青柠利合科技(北京)有限公司, All Rights Reserved.
 */
const registerPlugins = (plugins) => {
  return {
    install(app) {
      plugins.forEach((plugin) => {
        app.use(plugin);
      });
    },
  };
};

// 存储localStorage
const setStore = (name, content) => {
  if (!name) return;
  localStorage.setItem(name, JSON.stringify(content));
};

// 获取localStorage
const getStore = (name) => {
  if (!name) return;
  return JSON.parse(localStorage.getItem(name));
};

function removeSpace(value) {
  return value.replace(/\s+/g, "");
}
function formValidate(val, type) {
  const phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
  const emailReg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
  if (val === "") {
    return false;
  } else {
    // 非空验证
    if (type === "require") {
      return !!removeSpace(val);
    }
    if (type === "phone") {
      return phoneReg.test(val);
    }
    if (type === "email") {
      return emailReg.test(val);
    }
  }
}

// 获取url参数
function getUrlKey(name) {
  return (
    decodeURIComponent(
      (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
        location.href
      ) || [, ""])[1].replace(/\+/g, "%20")
    ) || null
  );
}

// 数组去重
function dedupe(array) {
  return Array.from(new Set(array));
}

const ModalHelper = ((bodyCls) => {
  let scrollTop;
  return {
    afterOpen: function () {
      scrollTop = document.scrollingElement.scrollTop;
      document.body.classList.add(bodyCls);
      document.body.style.top = -scrollTop + "px";
    },
    beforeClose: function () {
      document.body.classList.remove(bodyCls);
      // scrollTop lost after set position:fixed, restore it back.
      document.scrollingElement.scrollTop = scrollTop;
    },
  };
})("dialog-open");

export {
  registerPlugins,
  setStore,
  getStore,
  removeSpace,
  formValidate,
  getUrlKey,
  dedupe,
  ModalHelper,
};
